import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css' // 引入样式
import locale from 'element-plus/lib/locale/lang/zh-cn' // 引入中文
import VueWechatTitle from 'vue-wechat-title'// 引入页面设置title

const app = createApp(App)

/* 请求拦截器 */
axios.interceptors.request.use(request => {
  if (request.url !== 'api/auth/oauth/token') {
    // 将token设置成请求头
    if (store.state.Authorization) { request.headers = { Authorization: 'Bearer ' + localStorage.getItem('Authorization') } }
  }
  return request
}, error => { return Promise.reject(error) })

/* 响应拦截器 */
axios.interceptors.response.use(response => {
  if (response.data.code === 400) {
    localStorage.removeItem('Authorization')
    router.replace({ path: '/login' })
  }
  return response
}, error => {
  return Promise.reject(error)
})

// 将 axios 挂载到Vue原型对象上，Vue组件可通过this访问 axios
app.config.globalProperties.$axios = axios

app.use(VueWechatTitle).use(ElementPlus, { locale }).use(store).use(router).mount('#app')
